/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.rdb.datasource;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

/**
 * 数据源local类
 * 
 * @author mobing@b5m.com
 * @see com.b5m.bocean.holder.CRUDHoder
 * @version V0.0.1-SNAPSHOT, 2015-08-13下午1:17:20
 * @description
 * 
 */
public class DataSourceMapHolder {

	/** 对数据源进行线程缓存 */
	private static ThreadLocal<Map<String, DataSource>> local = new ThreadLocal<Map<String, DataSource>>();

	private static Map<String, DataSource> localStatic =new HashMap<String, DataSource>();
	/**
	 * 设置当前数据源
	 */
	public static void put(Map<String, DataSource> newNode) {
		if (newNode != null) {
		    Map<String, DataSource> oldNode = local.get();
			if (oldNode != newNode) {
			    local.remove();
			    local.set(newNode);
			    localStatic = newNode;
			}
		}
	}

	/**
	 * 获取当前线程的数据源
	 */
	public static Map<String, DataSource> get() {
	    
	    if(local.get() ==null){
	        return  localStatic;
	    }
		return local.get();
	}

	/**
	 * 移除当前数据源
	 */
	public static void remove() {
	    local.remove();
	}
	
}